﻿using System.Windows;
using System.Windows.Data;
using dIRca.Data.Services;
using dIRca.Data.Settings;
using dIRca.Extras.Services;
using GalaSoft.MvvmLight.Command;
using WP7Contrib.Logging;
using WP7Contrib.Services.Navigation;
using WP7Contrib.Services.Storage;

namespace dIRca.WP7.ViewModels
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class SettingsViewModel : ViewModelBaseWp7
    {
        #region Commands
        private bool _canSave;
        public bool CanSave
        {
            get { return _canSave; }
            set
            {
                if (_canSave == value)
                    return;

                var oldValue = _canSave;
                _canSave = value;
                SaveCommand.RaiseCanExecuteChanged();
                RaisePropertyChanged("CanSave", oldValue, CanSave, true);
            }
        }

        RelayCommand _saveCommand;
        public RelayCommand SaveCommand
        {
            get
            {
                return _saveCommand ?? (_saveCommand =
                    new RelayCommand(() => this.Save(), () => this.CanSave));
            }
        }

        private bool _canRestoreDefault;
        public bool CanRestoreDefault
        {
            get { return _canRestoreDefault; }
            set
            {
                if (_canRestoreDefault == value)
                    return;

                var oldValue = _canRestoreDefault;
                _canRestoreDefault = value;
                RestoreDefaultCommand.RaiseCanExecuteChanged();
                RaisePropertyChanged("CanRestoreDefault", oldValue, CanRestoreDefault, true);
            }
        }

        RelayCommand _restoreDefaultCommand;
        public RelayCommand RestoreDefaultCommand
        {
            get
            {
                return _restoreDefaultCommand ?? (_restoreDefaultCommand =
                    new RelayCommand(() => this.RestoreDefault(), () => this.CanRestoreDefault));
            }
        }

        RelayCommand _screenshotCommand;
        public RelayCommand ScreenshotCommand
        {
            get
            {
                return _screenshotCommand ?? (_screenshotCommand =
                    new RelayCommand(() => this.TakeScreenshot()));
            }
        }

        RelayCommand<FrameworkElement> _layoutRootCommand;
        public RelayCommand<FrameworkElement> LayoutRootCommand
        {
            get
            {
                return _layoutRootCommand ?? (_layoutRootCommand =
                    new RelayCommand<FrameworkElement>(sender => this.LayoutRoot = sender));
            }
        }
        #endregion

        #region Properties
        private readonly ISettingService _settingService;

        private string _pageTitle;
        public string PageTitle
        {
            get { return _pageTitle; }
            set
            {
                if (_pageTitle == value)
                    return;

                var oldValue = _pageTitle;
                _pageTitle = value;
                RaisePropertyChanged("PageTitle", oldValue, PageTitle, true);
            }
        }

        private Client _client;
        public Client Client
        {
            get { return _client; }
            set
            {
                if (_client == value)
                    return;

                var oldValue = _client;
                _client = value;
                RaisePropertyChanged("Client", oldValue, Client, true);
            }
        }

        public CollectionViewSource Networks { get; set; }
        public CollectionViewSource Servers { get; set; }

        public FrameworkElement LayoutRoot { get; private set; }
        #endregion

        /// <summary>
        /// Initializes a new instance of the SettingsViewModel class.
        /// </summary>
        public SettingsViewModel(INavigationService navigationService, IStorageService storageService, ILog log, ISettingService settingsService)
            : base(navigationService, storageService, log)
        {
            PageTitle = "Settings";
            if (IsInDesignMode)
            {
                // Code runs in Blend --> create design time data.
                Client = Factory.Create();
            }
            else
            {
                // Code runs "for real": Connect to service, etc...
                _settingService = settingsService;
                Client = _settingService.Settings;
            }
            Servers = new CollectionViewSource() { Source = Client.Servers };
            Networks = new CollectionViewSource() { Source = Client.Networks };
            CanSave = true;
            CanRestoreDefault = true;
        }

        void Save()
        {
            _settingService.Save(Client);
            CanRestoreDefault = true;
        }

        void RestoreDefault()
        {
            _settingService.RestoreDefault();
            Client = _settingService.Settings;
            Servers.Source = Client.Servers;
            Networks.Source = Client.Networks;
            Servers.View.Refresh();
            Networks.View.Refresh();
            CanRestoreDefault = false;
        }

        void TakeScreenshot()
        {
            var service = new Screenshots(LayoutRoot, App.Title + " " + PageTitle);
            service.Save();
        }

        public override void Cleanup()
        {
            // Clean own resources if needed

            base.Cleanup();
        }

        protected override void IsBeingActivated(IStorage storage)
        {
        }

        protected override void IsBeingDeactivated(IStorage storage)
        {
        }
    }
}